//
//  CustomSchemeHandler.m
//  Assistant
//
//  Created by 张继明 on 2019/9/4.
//  Copyright © 2019 张继明. All rights reserved.
//

#import "ITCustomSchemeHandler.h"

@interface ITCustomSchemeHandler()

@end

@implementation ITCustomSchemeHandler

- (void)webView:(WKWebView *)webView startURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask {
    NSString* scheme = urlSchemeTask.request.URL.scheme.lowercaseString;
    NSString* absoluteString = urlSchemeTask.request.URL.absoluteString.lowercaseString;
    NSString *extension = [absoluteString componentsSeparatedByString:@"."].lastObject;
    if ([scheme isEqualToString:@"bltest"]) {
        if ([extension isEqualToString:@"js"]) {
            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index.js" ofType:nil];
            NSData *data = [NSData dataWithContentsOfFile:filePath];
            NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:urlSchemeTask.request.URL statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"Content-Type" : @"text/plain"}];
            [urlSchemeTask didReceiveResponse:response];
            [urlSchemeTask didReceiveData:data];
            [urlSchemeTask didFinish];
        } else if ([extension isEqualToString:@"jpg"]) {
            NSString *path = [[NSBundle mainBundle] pathForResource:@"res1" ofType:@"png"];
            NSData *data = [NSData dataWithContentsOfFile:path];
            NSURLResponse *response = [[NSURLResponse alloc] initWithURL:urlSchemeTask.request.URL MIMEType:@"image/*" expectedContentLength:data.length textEncodingName:nil];
            [urlSchemeTask didReceiveResponse:response];
            [urlSchemeTask didReceiveData:data];
            [urlSchemeTask didFinish];
        }
    }
    
    NSLog(@"startURLSchemeTask %@    absoluteString %@ ",scheme,absoluteString);
}

- (void)webView:(WKWebView *)webView stopURLSchemeTask:(id<WKURLSchemeTask>)urlSchemeTask {
    
    
}

@end
